package T6_commite_update_2;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class T6_commite_notifyAll extends Thread {    
    public static void main(String[] args) {    
    	 Calculator calculator = new Calculator();    
         new ReaderResult(calculator).start();    
         new ReaderResult(calculator).start();    
         new ReaderResult(calculator).start();    
         calculator.start(); 
    }   
} 
class ReaderResult extends Thread {    
    Calculator c;    
    public ReaderResult(Calculator c) {    
    	this.c = c;    
    }    
    public void run() {    
    	try {
			Thread.sleep(2000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
//    	synchronized (c) {    
		Util.lock.lock();
    		try {    
    			System.out.println(Thread.currentThread() + "等待计算结果。。。");    
//    				c.wait();    
    			Util.read.await();
    		} catch (InterruptedException e) {    
    			e.printStackTrace();    
    		}    
    		System.out.println(Thread.currentThread() + "计算结果为：" + c.total);   
    		
    	Util.lock.unlock();
//    	}    
    }      
}  

class Calculator extends Thread {    
    int total;    
    public void run() {    
    	Util.lock.lock();
    	for (int i = 0; i < 101; i++) {    
    		total += i;    
    	} 
    	Util.read.signalAll();
    	Util.lock.unlock();
    }    
}

class Util {
	final static Lock lock = new ReentrantLock();
	final static Condition read  = lock.newCondition(); 
}
